/**********************************************************/
/* Goblin Defense - A tower defense game for Wii Homebrew */
/* Copyright (C) 2009 Ricardo Bueno Cordeiro              */
/* Licensed under de GNU General Public License v2.0      */
/*   See license_gplv2.txt for detalis                    */
/**********************************************************/

#ifndef __TRANS_ARRAY_H_
#define __TRANS_ARRAY_H_

#include <gccore.h>

template <class T, u32 SIZE>
class TransArray {
private:
	typedef T *element_p;

	u32 num;
	u32 num_new;
	element_p itens[SIZE];

	void pack() {
		u32 j = 0;

		for (u32 i = 0; i < num; i++) {
			if (itens[i] != NULL) {
				itens[j] = itens[i];
				j++;
			}
		}

		num = j;		
	}
	
public:
	TransArray() : num(0), num_new(0) {}

	int add(element_p e) {
		if ((num + num_new) >= SIZE) {
			delete e;
			return 0;
		}
		itens[num + num_new++] = e;
		return 1;
	}
	
	void commit() {
		if (num_new > 0) {
			num += num_new;
			num_new = 0;
		}
		pack();
	}
	
	void remove(u32 i) {
		delete this->itens[i];
		this->itens[i] = NULL;
	}
	
	element_p &operator[](u32 i) {
		return this->itens[i];
	}
	
	u32 size() {
		return num;
	}
};

#endif
